PHP 여러개의 연속된 유튜브 주소 URL 파싱 추출
하우코드
본문
주소를 여러개 동시에 붙여서 추출하기
$str="
https://youtube.com/v/1112LHyy5Qo
https://youtube.com/?v=1132LHyy5Qo
https://youtube.com/watch?v=1152LHyy5Qo
https://youtu.be/1168Y8fF176
https://www.youtube.com/watch?v=1182LHyy5Qo&t=1527shttps://www.youtube.com/watch?v=119JSIBJORYhttps://youtu.be/cKVaUtl0vHs";
echo "<xmp>";
$str=str_ireplace('https://',"\nhttps://",$str);
echo $str;
//안되는 패턴 : $pa='(http(s|):|)\/\/(www\.|)yout(.*?)\/(embed\/|watch.*?v=|)([a-z_A-Z0-9\-]{11})';
//안되는 패턴 : $pa='https?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))';//x
//안되는 패턴 : $pa='^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*';//x
//안되는 패턴 : =&+%\w-]*";$pa="https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com\S*[^\w\-\s])([\w\-]{11})(?=[^\w\-]|$)(?![?=&+%\w]*(?:['\"][^<>]*>|<\/a>))[?=&+%\w-]*";//
$pa="https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com\S*[^\w\-\s])([\w\-]{11})";//
preg_match_all("/$pa/si",$str,$match);
var_dump($match);
echo "</xmp>";
$youtube_regexp = "/^http:\/\/(?:www\.)?(?:youtube.com|youtu.be)\/(?:watch\?(?=.*v=([\w\-]+))(?:\S+)?|([\w\-]+))$/";
// The test urls, one per line.
$urls = <<<EOF
http://www.youtube.com/watch?v=4-iI6UnKUCs&feature=grec_index
http://www.youtube.com/watch?v=4-iI6UnKUCs
http://www.youtube.com/watch?v=QNnz_ktVggQ&NR=1
EOF;
---------------------
처리결과
https://youtube.com/v/1112LHyy5Qo
https://youtube.com/?v=1132LHyy5Qo
https://youtube.com/watch?v=1152LHyy5Qo
https://youtu.be/1168Y8fF176
https://www.youtube.com/watch?v=1182LHyy5Qo&t=1527s
https://www.youtube.com/watch?v=119JSIBJORY
https://youtu.be/cKVaUtl0vHsarray(2) {
[0]=>
array(7) {
[0]=>
string(33) "https://youtube.com/v/1112LHyy5Qo"
[1]=>
string(34) "https://youtube.com/?v=1132LHyy5Qo"
[2]=>
string(39) "https://youtube.com/watch?v=1152LHyy5Qo"
[3]=>
string(28) "https://youtu.be/1168Y8fF176"
[4]=>
string(43) "https://www.youtube.com/watch?v=1182LHyy5Qo"
[5]=>
string(43) "https://www.youtube.com/watch?v=119JSIBJORY"
[6]=>
string(28) "https://youtu.be/cKVaUtl0vHs"
}
[1]=>
array(7) {
[0]=>
string(11) "1112LHyy5Qo"
[1]=>
string(11) "1132LHyy5Qo"
[2]=>
string(11) "1152LHyy5Qo"
[3]=>
string(11) "1168Y8fF176"
[4]=>
string(11) "1182LHyy5Qo"
[5]=>
string(11) "119JSIBJORY"
[6]=>
string(11) "cKVaUtl0vHs"
}
}
- 이전글Embed youtube “Shorts” videos 22.08.18
- 다음글정규식 구문 및 예제 22.07.06
댓글목록
등록된 댓글이 없습니다.